#Mount-VHD
Param ([String]$vhdPath)

$ImgMgtSvc = Get-WmiObject -NameSpace  "root\virtualization" -Class "MsVM_ImageManagementService"
$result=$ImgMgtSvc.mount($vhdPath)
if ($result.returnValue -eq 4096) 
      {$Job = [WMI]$result.job
       while ($job.jobstate -eq 4) {Start-Sleep -seconds 1 ; $Job.PSBase.Get() }  
       Start-sleep 2
       $MountedDiskImage= Get-WmiObject -NameSpace "root\virtualization" -query ("Select * from Msvm_MountedStorageImage where name ='"+  $Vhdpath.replace("\","\\")  +"'")
       $diskIndex=(Get-WmiObject -Query "Select * From win32_diskdrive Where Model='Msft Virtual Disk SCSI Disk Device' and ScsiTargetID=$($MountedDiskImage.TargetId) and ScsiLogicalUnit=$($MountedDiskImage.Lun) and scsiPort=$($MountedDiskImage.PortNumber)").index
       if ($diskIndex -ne $null) {@("select disk $diskIndex", "online disk" , "attributes disk clear readonly", "exit")  | Diskpart | Out-Null}
      }

